import axios from 'axios'

const state = {
  sourceInspect: [],
  sourceRectify: []

}

const mutations = {
  SAVE_INSPECT: (state, data) => {
    state.sourceInspect = data;
  },
  SAVE_RECTIFY:(state, data) => {
    state.sourceRectify = data;
  }
}

const actions = {
  queryInspect({ commit, rootState }, { filter }) {
    let user = rootState.login.userbase;
    return new Promise((resolve, reject) => {
      axios.post('api/query_inspect_record/', { user, filter }).then(response => {
        if (response.data.status === 0) {
          commit('SAVE_INSPECT', response.data.content)
          resolve(response.data.content);
        } else {
          console.log(response);
          resolve();
        }
      }).catch(error => {
        console.log(error);
        reject(error);
      })
    })
  },
  queryRectify({ commit, rootState }, { filter }) {
    let user = rootState.login.userbase;
    return new Promise((resolve, reject) => {
      axios.post('api/query_rectify_record/', { user, filter }).then(response => {
        if (response.data.status === 0) {
          commit('SAVE_RECTIFY', response.data.content)
          resolve(response.data.content);
        } else {
          console.log(response);
          resolve();
        }
      }).catch(error => {
        console.log(error);
        reject(error);
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}